package com.ruoyi.order.service.impl;

import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.MyUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.customer.domain.CustomerDistribution;
import com.ruoyi.customer.mapper.CustomerDistributionMapper;
import com.ruoyi.order.domain.OrderCashDistribution;
import com.ruoyi.order.mapper.OrderCashDistributionMapper;
import com.ruoyi.order.service.IOrderCashDistributionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.validation.Validator;
import java.util.List;

/**
 * 客户分销提现订单 业务层处理
 * 
 * @author ruoyi
 */
@Service
public class OrderCashDistributionServiceImpl implements IOrderCashDistributionService
{
    @Autowired
    private OrderCashDistributionMapper orderCashDistributionMapper;

    @Autowired
    private CustomerDistributionMapper customerDistributionMapper;

    @Autowired
    protected Validator validator;

    /**
     * 根据条件分页查询客户分销提现订单列表
     * 
     * @param orderCashDistribution 客户分销提现订单信息
     * @return 客户分销提现订单信息集合信息
     */
    @Override
    public List<OrderCashDistribution> selectOrderCashDistributionList(OrderCashDistribution orderCashDistribution)
    {
        return orderCashDistributionMapper.selectOrderCashDistributionList(orderCashDistribution);
    }

    /**
     * 通过客户分销提现订单编号查询客户分销提现订单
     * 
     * @param orderNo 客户分销提现订单编号
     * @return 客户分销提现订单对象信息
     */
    @Override
    public OrderCashDistribution selectOrderCashDistributionByOrderNo(String orderNo)
    {
        return orderCashDistributionMapper.selectOrderCashDistributionByOrderNo(orderNo);
    }

    /**
     * 新增保存客户分销提现订单信息
     * 
     * @param orderCashDistribution 客户分销提现订单信息
     * @return 结果
     */
    @Override
    @Transactional
    public int insertOrderCashDistribution(OrderCashDistribution orderCashDistribution)
    {
        // 新增客户分销提现订单信息
        Long userId = SecurityUtils.getUserId();
        CustomerDistribution customerDistribution = customerDistributionMapper.selectUserById(userId);
        if(customerDistribution.getBalance().compareTo(orderCashDistribution.getAmount()) == -1){
            throw new ServiceException("提现失败：可用余额不足");
        }
        String orderNo = MyUtils.getOrderNoByTimeAndId(userId);
        orderCashDistribution.setOrderNo(orderNo);
        orderCashDistribution.setUserId(userId);
        orderCashDistribution.setStatus(UserConstants.STATUS_AUDIT);
        // 更新店员余额 减去
        customerDistribution.setBalance(customerDistribution.getBalance().subtract(orderCashDistribution.getAmount()));
        customerDistributionMapper.updateUserMoney(customerDistribution);
        int rows = orderCashDistributionMapper.insertOrderCashDistribution(orderCashDistribution);
        return rows;
    }

    /**
     * 修改保存客户分销提现订单信息
     * 
     * @param orderCashDistribution 客户分销提现订单信息
     * @return 结果
     */
    @Override
    @Transactional
    public int updateOrderCashDistribution(OrderCashDistribution orderCashDistribution)
    {
        // 驳回
        if(orderCashDistribution.getStatus().equals(UserConstants.STATUS_REJECT)){
            CustomerDistribution customerDistribution = customerDistributionMapper.selectUserById(orderCashDistribution.getUserId());
            // 更新余额 加
            customerDistribution.setBalance(customerDistribution.getBalance().add(orderCashDistribution.getAmount()));
            customerDistributionMapper.updateUserMoney(customerDistribution);
        }
        // 通过
        if(orderCashDistribution.getStatus().equals(UserConstants.STATUS_APPROVE)){
            CustomerDistribution customerDistribution = customerDistributionMapper.selectUserById(orderCashDistribution.getUserId());
            // 用户已提佣金 加
            customerDistribution.setMoneyGet(customerDistribution.getMoneyGet().add(orderCashDistribution.getAmount()));
            customerDistributionMapper.updateUserMoney(customerDistribution);
        }
        return orderCashDistributionMapper.updateOrderCashDistribution(orderCashDistribution);
    }

}
